package guru.springframework.services;


import guru.springframework.domain.ProductB;
import guru.springframework.dto.MockGetRequest;
import guru.springframework.dto.MockGetResponse;
import guru.springframework.mappers.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Service
public class HttpService {


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    public void setProductRepository(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    private String url="http://localhost:8080/product/mockGet";

    public  String getProductsByGet(Integer id) {

        ProductB product = productMapper.selectProductById(id);
        MockGetRequest mockGetRequest = new MockGetRequest();

        String method = "get";
        String uri = product.getUrl();

        mockGetRequest.setMethod(method);
        mockGetRequest.setUrl(uri);
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers =new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity request=new HttpEntity(mockGetRequest,headers);

        MockGetResponse result = restTemplate.postForObject(url,mockGetRequest,MockGetResponse.class);

        return result.getMessage();
    }


}
